home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 9 / FM Towns Free Software Collection 9.iso / t_os / shell / tsbgex / src / neko / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  5.0 KB  |  251 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h> 
  4. #include <winb.h>
  5. #include <te.h>
  6. #include <fntb.h>
  7. #include <gui.h>
  8. #include <egb.h>
  9. #include <guidbg.h>
  10. #include <file_dlg.h>
  11. #include "neko.h"
  12.  
  13. int aplid;
  14.  
  15. void main(int argc, char *argv[])
  16. {
  17.     MMICTRL ctrl;
  18.     extern int APL_init();
  19.  
  20.     ctrl.displayPage = SCREENAVAILABLE;
  21.     ctrl.mode = SCREENAVAILABLE;
  22.     ctrl.page0 = SCREENIGNORE;
  23.     ctrl.page1 = SCREENIGNORE;
  24.     ctrl.size = 0;
  25.     ctrl.ptr = NULL;
  26.     ctrl.asize = 0;
  27.     ctrl.aptr = NULL;
  28.     ctrl.white = 15;
  29.     ctrl.black = 8;
  30.     ctrl.gray = 7;
  31.     ctrl.xor = 7;
  32.     
  33.     if (MMI_Open(&ctrl) != NOERR){
  34.         MMI_Close();
  35.         return;
  36.     }
  37.     if (APL_init(argc, argv) != NOERR){
  38.         MMI_Close();
  39.         return;
  40.     }
  41.     MMI_ExecSystem();
  42.     MMI_Close();
  43. }
  44.  
  45. int task_mode = 0x03;
  46. int do_unmap = FALSE;
  47.  
  48. APL_init(int argc, char **argv)
  49. {
  50.     extern int timer, frame16, frame32k;
  51.     extern int userFunc();
  52.     extern void nekomain();
  53.     extern void active_check();
  54.  
  55.     while (--argc > 0){
  56.         argv++;
  57.         if (strcmp(*argv, "-t") == 0  &&  argc > 1){
  58.             timer = atoi(*++argv);
  59.             argc--;
  60.         } else if (strcmp(*argv, "-m") == 0  &&  argc > 1){
  61.             task_mode = atoi(*++argv);
  62.             argc--;
  63.         } else if (strcmp(*argv, "-unmap") == 0){
  64.             do_unmap = TRUE;
  65.         } else if (strcmp(*argv, "-nf") == 0){
  66.             frame16 = frame32k = FALSE;
  67.         }
  68.     }
  69.     aplid = MMI_GetApliId();
  70.     active_check(TRUE);
  71.     MMI_SendMessage(MMI_GetBaseObj(), MM_SETEXEC, 1, userFunc);
  72.     MMI_CallMessage(aplid, GM_TITLE, (int)"NEKO", 0);
  73.     if (task_mode & 0x01)
  74.         MMI_SetIdleTaskFunc(nekomain);
  75.     if (task_mode & 0x02)
  76.         MMI_SetIntervalFunc(nekomain);
  77.     MMI_CallMessage(aplid, GM_SLEEP, 0, 0);
  78.     return (NOERR);
  79. }
  80.  
  81. int (**winfunc)();
  82.  
  83. connect_server()
  84. {
  85.     int server;
  86.  
  87.     if ((server = MMI_CallMessage(aplid, GM_QUERYID, QM_SAMEAS, (int)SERVER))
  88.                 > NOERR){
  89.         if (MMI_CallMessage(server, GM_EXECUSER, 0, 0) != MAGIC)
  90.             server = NOERR; /* not ready */
  91.         else
  92.             winfunc = (int (**)())MMI_CallMessage(server, GM_EXECUSER, 1, 0);
  93. #ifdef DEBUG
  94.         printf("NEKO: server = %d, winfunc = 0x%x\n", server, winfunc);
  95. #endif
  96.     } else {
  97. #ifdef DEBUG
  98.         printf("NEKO: server = %d\n", server);
  99. #endif
  100.         server = NOERR;
  101.     }
  102.     return (server);
  103. }
  104.  
  105. int server = NOERR;
  106. int server_die = FALSE;
  107. int own = FALSE;
  108. int active = FALSE;
  109. int restart = TRUE;
  110. int unmapped = FALSE;
  111. short vramseg;
  112. u_long vramoff;
  113. int pixel, fbwidth, dwidth, vheight;
  114. RESOLUTION mode = { 0, 0, 0, 0, 0, 0, };
  115.  
  116. void active_check(int init)
  117. {
  118.     int owner;
  119.     char *title;
  120.     SCRNDATA screen;
  121.     extern int neko_first, neko_count;
  122.     extern int modecmp();
  123.     extern void setup();
  124.     
  125.     if (server == NOERR){
  126.         if ((server = connect_server()) > NOERR){
  127.             init = TRUE;
  128.             restart = TRUE;
  129.             neko_first = TRUE;
  130.             neko_count = 1;
  131.         }
  132.     }
  133.     if (server <= NOERR){
  134.         server = NOERR;
  135.         restart = TRUE;
  136.         neko_count = 1;
  137.         return;
  138.     }
  139.     owner = MMI_CallMessage(aplid, GM_QUERYID, QM_BACKPAGE, 0);
  140.     title = (char *)MMI_CallMessage(owner, GM_TITLE, (int)NULL, 0);
  141. #ifdef DEBUG
  142.     printf("NEKO: BACKPAGE owner is %s\n", title);
  143. #endif
  144.     MMI_CallMessage(owner, GM_SCRNDATA, FALSE, (int)&screen);
  145.     if (init){
  146.         if (strcmp(title, OWNER) == 0){
  147.             own = TRUE;
  148.             mode = screen.page[1];
  149.             setup(&screen.page[1]);
  150.         } else {
  151.             own = active = FALSE;
  152.         }
  153.         return;
  154.     }
  155.     if (own){
  156.         if (strcmp(title, OWNER) == 0){
  157.             if (modecmp(&mode, &screen.page[1]) != 0){
  158. #ifdef DEBUG
  159.                 printf("NEKO: mode change\n");
  160. #endif
  161.                 mode = screen.page[1];
  162.                 setup(&screen.page[1]);
  163.             }
  164.         } else {
  165.             own = active = FALSE;
  166.         }
  167.     } else {
  168.         if (strcmp(title, OWNER) == 0){
  169.             own = TRUE;
  170.             setup(&mode);
  171.         }
  172.     }
  173. #ifdef DEBUG
  174.     printf("NEKO: own=%d, active=%d, restart=%d\n", own, active, restart);
  175. #endif
  176. }
  177.     
  178. void setup(RESOLUTION *page1)
  179. {
  180.     extern int neko_count;
  181.  
  182.     restart = TRUE;
  183.     neko_count = 1;
  184.     pixel = page1->pixel;
  185.     fbwidth = page1->byte;
  186.     dwidth = page1->dx;
  187.     vheight = page1->vy;
  188.     vramseg = page1->segment;
  189.     vramoff = (u_long)page1->offset;
  190.     active = (pixel == 4  ||  pixel == 16);
  191.     unmapped = FALSE;
  192. }
  193.  
  194. modecmp(RESOLUTION *a, RESOLUTION *b)
  195. {
  196.     return (memcmp(a, b, sizeof (RESOLUTION) - sizeof (short)));
  197. }
  198.  
  199. userFunc(int apliId, int messId, int info, int data)
  200. {
  201.     int    ret;
  202.     extern int neko_count;
  203.     extern void terminate(), map();
  204.  
  205.     ret = ILLEGAL_FUNCTION ;
  206.  
  207.     switch (messId){
  208.         case GM_EXECUSER:    /* server die */
  209. #ifdef DEBUG
  210.             printf("NEKO: server die\n");
  211. #endif
  212.             server_die = TRUE;
  213.             server = NOERR;
  214.             active = FALSE;
  215.             terminate();
  216.             break;
  217.         case GM_PRESCRCHG: 
  218. #ifdef DEBUG
  219.             printf("NEKO: POSTSCRCHG\n");
  220. #endif
  221.             if (!server_die  &&  do_unmap){
  222.                 map(FALSE);
  223.                 neko_count = 1;
  224.             }
  225.             ret = NOERR;
  226.             break;
  227.         case GM_POSTSCRCHG:
  228. #ifdef DEBUG
  229.             printf("NEKO: POSTSCRCHG\n");
  230. #endif
  231.             if (server_die)
  232.                 server_die = FALSE;
  233.             else
  234.                 active_check(FALSE);
  235.             ret = NOERR;
  236.             break;
  237.         case GM_QUIT:
  238.             terminate();
  239.             MMI_SetHaltFlag(TRUE);
  240.             ret = NOERR;
  241.             break;
  242.         case GM_PURGE:
  243.         case GM_ENVIRONMENT:
  244.         case GM_SHOW:
  245.         case GM_ERASE: 
  246.             ret = NOERR;
  247.             break;
  248.     }
  249.     return (ret);
  250. }
  251.